package com.novellotus.iov.carvice.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.PersonBean;
import com.novellotus.iov.carvice.model.PersonalModel;
import com.novellotus.iov.carvice.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 个人信息页面
 */
public class PersonalActivity extends BaseActivity implements PersonalModel.PersonInterface, LoadingView.OnBtnClickListener {

    @BindView(R.id.btn_right)
    Button btn_right;
    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.tv_area)
    TextView tv_area;
    @BindView(R.id.edt_name)
    EditText edt_name;
    @BindView(R.id.tv_email)
    TextView tv_email;
    @BindView(R.id.tv_mobile)
    TextView tv_mobile;
    @BindView(R.id.loadingView)
    LoadingView loadingView;
    PersonalModel personalModel;

    public String code = "";

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_personal);
    }

    @Override
    public void init() {
        tv_tittle.setText("个人信息");
        btn_right.setText("保存");
        btn_right.setTextColor(getResources().getColor(R.color.gray_line));
        btn_right.setClickable(false);
        loadingView.mWarnView.setTextColor(getResources().getColor(R.color.text_black));
        loadingView.setLoadingModel(LoadingView.MODEL_DEFAULT);
        loadingView.setBackgroundColor(getResources().getColor(R.color.white));
        loadingView.setOnBtnClickListener(this);
        loadingView.loading();
        personalModel = new PersonalModel(this, this);
        personalModel.getPersonalData();
        personalModel.setNameEdit(edt_name, btn_right);


    }

    @OnClick(R.id.img_back)
    void back() {
        MyUtils.hideInput(this);
        finish();
    }

    @OnClick(R.id.rlt_city)
    void chooseCity() {
        MyUtils.hideInput(this);
        personalModel.showCityPicker(tv_area);
    }

    @OnClick(R.id.btn_right)
    void saveMessage() {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("nickName", edt_name.getText().toString());
            requestData.put("area", code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyUtils.logInformation("保持:" + edt_name.getText().toString());
        personalModel.saveMessage(requestData.toString(), edt_name.getText().toString());
    }

    @Override
    public void setMessage(PersonBean personBean) {
        try {
            loadingView.success();
            edt_name.setText(personBean.getNickName());
            edt_name.setSelection(personBean.getNickName().length());
            if (personBean.getCity() != null) {
                if (personBean.getCity().getChildren() != null) {
                    tv_area.setText(personBean.getCity().getChildren().getLabel() + " " + personBean.getCity().getLabel());
                    code = personBean.getCity().getValue() + "";
                } else {
                    code = personBean.getCity().getValue() + "";
                    tv_area.setText(personBean.getCity().getLabel());
                }
            }
            tv_email.setText(personBean.getEmail());
            tv_mobile.setText(personBean.getMobile());
        } catch (Exception e) {
            MyUtils.logInformation(e.toString());
        }

    }

    @Override
    public void getMessageFail(String msg) {
        loadingView.empty(msg, true);
    }

    @Override
    public void setCityCode(String code) {
        this.code = code;
        if (edt_name.getText().length() >= 2) {
            btn_right.setClickable(true);
            btn_right.setTextColor(getResources().getColor(R.color.white));
        }
    }

    @Override
    public void onReload() {
        loadingView.loading();
        personalModel.getPersonalData();
    }
}
